//
//  ViewController.m
//  CPICApp
//
//  Created by lizhiwei on 13-2-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "ViewController.h"
#import "AppDelegate.h"
#import "Config.h"
#import "WeatherCustomerView.h"
#import "UIImageView+WebCache.h"
#import "SkinUpdateInterface.h"
#import "RCAlertViewUtil.h"
#import "RCSoftUpdate.h"
#import "RCUpdateNotice.h"
#import "ServiceNoticeRootViewController.h"

#define IF_SKIN_UPDATE @"IF_SKIN_UPDATE"

@interface ViewController ()

@end

@implementation ViewController
@synthesize youkuMenuView;
@synthesize lblUserName;
@synthesize btnNotice;
@synthesize btnCancel;
@synthesize LoginInOrOut;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"main_bg@2x"]];
    
    self.view.autoresizesSubviews=TRUE;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin
    | UIViewAutoresizingFlexibleRightMargin |  UIViewAutoresizingFlexibleBottomMargin
    | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    
    NSUserDefaults *accountDefault = [NSUserDefaults standardUserDefaults];
    NSString *IfUpdate= [accountDefault objectForKey:@"IF_SKIN_UPDATE"];
    
    if ([IfUpdate isEqualToString:@"1"])
    {
        SkinUpdateInterface *skinupdate =[[SkinUpdateInterface alloc]init];
        [skinupdate setSkinUpdateRequest];
        [skinupdate setHidden:YES];
        [self.view addSubview:skinupdate];
        [skinupdate release];
    }
    
    [AppDelegate app].unc.navigationBarHidden = TRUE;
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    UIImageView * LogoImage = [[UIImageView alloc]init];
    LogoImage.image = [UIImage imageNamed:@"main_bg@2x"];
    NSLog(@"-----3333333333%@",bgimage);
    if ([bgimage isEqualToString:@"蓝色主题风格"]) 
    {
         LogoImage.image = [UIImage imageNamed:@"main_bg@2x"];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        LogoImage.image = [UIImage imageNamed:@"main_bg1@2x"];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSURL *urlSkinImg=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_01]];
        
        [LogoImage setImageWithURL:urlSkinImg success:^(UIImage *image) 
         {
             imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_01];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        NSData *dataImg=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_01];
        LogoImage.image=[UIImage imageWithData:dataImg];
    }
    
    LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, self.view.frame.size.height);
    [self.view addSubview:LogoImage];
    [LogoImage release];

    lblUserName=[[UILabel alloc]initWithFrame:CGRectMake(200, 0, 80, 50)];
    lblUserName.backgroundColor=[UIColor clearColor];
    lblUserName.font=[UIFont systemFontOfSize:18.0];
    [self.view addSubview:lblUserName];
    

    
    int iphone5Height;
    if (IS_IPHONE_5) {
        iphone5Height=50;
    }else{
        iphone5Height=0;
    }
    btnNotice=[[UIButton alloc]initWithFrame:CGRectMake(30, 290+iphone5Height, 200, 50)];
    btnNotice.backgroundColor=[UIColor clearColor];
    btnNotice.titleLabel.textColor=[UIColor whiteColor];
    btnNotice.titleLabel.font=[UIFont boldSystemFontOfSize:18.0];
    [btnNotice setShowsTouchWhenHighlighted:YES];
    [btnNotice addTarget:self action:@selector(gotoServiceNoticeView) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btnNotice];
    
    WeatherCustomerView * weatherview = [[WeatherCustomerView alloc]initWithFrame:CGRectMake(0, 0, 320, 200)];
    [self.view addSubview:weatherview];
    [weatherview release];
    
    youkuMenuView = [[HHYoukuMenuView alloc]initWithFrame:[HHYoukuMenuView getFrame]];
    [self.view addSubview:youkuMenuView];
    [youkuMenuView release];
    
    btnCancel=[UIButton buttonWithType:UIButtonTypeCustom];
    btnCancel.frame=CGRectMake(270, 6, 47, 32);
    //[btnCancel setTitle:@"退出" forState:UIControlStateNormal];
    //[btnCancel setImage:[UIImage imageNamed:@"Loginout.png"] forState:UIControlStateNormal];
    [btnCancel addTarget:self action:@selector(doCancel) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnCancel];
    
    NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
    NSLog(@"userinfodefault%@",[myDefaults objectForKey:DICT_USER_INFO]);
    NSDictionary *dictUserInfo=[[NSDictionary alloc]initWithDictionary:[myDefaults dictionaryForKey:DICT_USER_INFO]];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserName"]) {
            self.lblUserName.text=[dictUserInfo valueForKey:@"UserName"];
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserName"]);
            self.btnCancel.hidden=NO;
            LoginInOrOut=NO;
            [btnCancel setImage:[UIImage imageNamed:@"Loginout.png"] forState:UIControlStateNormal];
        }
        else
        {
             self.lblUserName.text=@"";
            //self.btnCancel.hidden=YES;
            LoginInOrOut=YES;
            [btnCancel setImage:[UIImage imageNamed:@"LoginIN.png"] forState:UIControlStateNormal];
        }
    }
    else
    {
        self.lblUserName.text=@"";
        LoginInOrOut=YES;
        [btnCancel setImage:[UIImage imageNamed:@"LoginIN.png"] forState:UIControlStateNormal];
        //self.btnCancel.hidden=YES;
    }
    [dictUserInfo release];
    
    [self getNoticeNum];
    [self updateVersion];
}

-(void)doCancel
{
    if (LoginInOrOut==NO) {
        UIAlertView *myAlert=[[UIAlertView alloc]initWithTitle:@"注意" message:@"你确定要注销并退出当前用户吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        myAlert.tag=1;
        [myAlert show];
        [myAlert release];
    }
    else if(LoginInOrOut==YES)
    {
        LoginViewController *loginViewCtrl=[[LoginViewController alloc]init];
        [[AppDelegate app].unc pushViewController:loginViewCtrl animated:YES];
        [loginViewCtrl release];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
        if (alertView.tag=1) {
            if (buttonIndex==0) {
            }
            else if(buttonIndex==1)
            {
                //清空缓存数据
                NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
                [myDefaults setObject:nil forKey:DICT_CUSTOMER_INFO];
                [myDefaults setObject:nil forKey:DICT_USER_INFO];
                [myDefaults synchronize];
                //
                exit(1);
            }
        }

}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    [youkuMenuView release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)gotoServiceNoticeView
{
    NSUserDefaults *myDefaults=[NSUserDefaults standardUserDefaults];
    NSDictionary *dictUserInfo=[[NSDictionary alloc]initWithDictionary:[myDefaults dictionaryForKey:DICT_USER_INFO]];
    if (dictUserInfo!=nil) {
        if ([dictUserInfo valueForKey:@"UserName"]) {
            self.lblUserName.text=[dictUserInfo valueForKey:@"UserName"];
            NSLog(@"%@",[dictUserInfo valueForKey:@"UserName"]);
        }
        else
        {
            return;
        }
    }
    else
    {
        return;
    }
    
    if (self.btnNotice.titleLabel.text.length==0) {
        return;
    }
    ServiceNoticeRootViewController *serviceNoticeRootViewCtrl=[[ServiceNoticeRootViewController alloc]init];
    [[AppDelegate app].unc pushViewController:serviceNoticeRootViewCtrl animated:YES];
    [serviceNoticeRootViewCtrl release];
    
}
-(void)getNoticeNum
{
    RCUpdateNotice *rcUpdateNotice=[[RCUpdateNotice alloc]initUpdateNoticeView];
    [rcUpdateNotice setNoticeRequest];
    [rcUpdateNotice setHidden:YES];
    [self.view addSubview:rcUpdateNotice];
    [rcUpdateNotice release];
}

-(void)updateVersion
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    RCSoftUpdate *rcsoftupdate =[[RCSoftUpdate alloc]initSoftView];
    [rcsoftupdate setSoftUpdateRequest];
    [rcsoftupdate setHidden:YES];
    [self.view addSubview:rcsoftupdate];
    [rcsoftupdate release];
    [pool release];
}

-(void)dealloc
{
    [lblUserName release];
    [btnNotice release];
    [btnCancel release];
    [super dealloc];
}
@end
